/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JMDIApplication.java
 *
 * Created on 2009-9-29, 15:39:54
 */
package net.liuxuan.OSMC.client;

import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import net.liuxuan.OSMC.component.LenghtLimitedDocument;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

/**
 * 
 * @author Administrator
 */
public class JMDIApplication extends JFrame implements SwingClientHandler.Callback {

    /** Creates new form JMDIApplication */
    public JMDIApplication() {

        try {
            // 设置视觉样式
            String version = System.getProperty("java.version");
            if (version.startsWith("1.6")) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } else {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        userinitComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        statusbar = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        logFrame = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        LogTextPane = new javax.swing.JTextPane();
        chatFrame = new javax.swing.JInternalFrame();
        ChatSenderTextField = new javax.swing.JTextField();
        ChatSendButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        ChatLogTextArea = new javax.swing.JTextArea();
        loginFrame = new javax.swing.JInternalFrame();
        ServerAddressLabel = new javax.swing.JLabel();
        ServerPortLabel = new javax.swing.JLabel();
        ServerAddressTextField = new javax.swing.JTextField();
        ServerPortTextField = new javax.swing.JTextField();
        LogInButton = new javax.swing.JButton();
        LogOutButton = new javax.swing.JButton();
        UserNameLabel = new javax.swing.JLabel();
        UserNameTextField = new javax.swing.JTextField();
        UserPassLabel = new javax.swing.JLabel();
        UserPassTextField = new javax.swing.JPasswordField();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        viewMenu = new javax.swing.JMenu();
        LoginMenuItem = new javax.swing.JMenuItem();
        chatMenuItem = new javax.swing.JMenuItem();
        logMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("net/liuxuan/OSMC/client/Bundle"); // NOI18N
        setTitle(bundle.getString("JMDIApplication.title")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setName("jframemain"); // NOI18N

        statusbar.setFocusCycleRoot(true);
        statusbar.setFocusable(false);

        org.jdesktop.layout.GroupLayout statusbarLayout = new org.jdesktop.layout.GroupLayout(statusbar);
        statusbar.setLayout(statusbarLayout);
        statusbarLayout.setHorizontalGroup(
            statusbarLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE)
        );
        statusbarLayout.setVerticalGroup(
            statusbarLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusbarLayout.createSequentialGroup()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jDesktopPane1.setBackground(new java.awt.Color(204, 204, 204));
        jDesktopPane1.setAutoscrolls(true);
        jDesktopPane1.setDoubleBuffered(true);

        logFrame.setClosable(true);
        logFrame.setIconifiable(true);
        logFrame.setMaximizable(true);
        logFrame.setResizable(true);
        logFrame.setTitle(bundle.getString("JMDIApplication.logFrame.title")); // NOI18N

        jScrollPane1.setViewportView(LogTextPane);

        org.jdesktop.layout.GroupLayout logFrameLayout = new org.jdesktop.layout.GroupLayout(logFrame.getContentPane());
        logFrame.getContentPane().setLayout(logFrameLayout);
        logFrameLayout.setHorizontalGroup(
            logFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(logFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addContainerGap())
        );
        logFrameLayout.setVerticalGroup(
            logFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(logFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 306, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        logFrame.setBounds(70, 50, 460, 400);
        jDesktopPane1.add(logFrame, javax.swing.JLayeredPane.DEFAULT_LAYER);

        chatFrame.setClosable(true);
        chatFrame.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        chatFrame.setIconifiable(true);
        chatFrame.setMaximizable(true);
        chatFrame.setResizable(true);
        chatFrame.setTitle(bundle.getString("JMDIApplication.chatFrame.title")); // NOI18N
        chatFrame.setVisible(true);

        ChatSenderTextField.setText(bundle.getString("JMDIApplication.ChatSenderTextField.text")); // NOI18N

        ChatSendButton.setText(bundle.getString("JMDIApplication.ChatSendButton.text")); // NOI18N
        ChatSendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChatSendButtonActionPerformed(evt);
            }
        });

        ChatLogTextArea.setColumns(20);
        ChatLogTextArea.setRows(5);
        jScrollPane2.setViewportView(ChatLogTextArea);

        org.jdesktop.layout.GroupLayout chatFrameLayout = new org.jdesktop.layout.GroupLayout(chatFrame.getContentPane());
        chatFrame.getContentPane().setLayout(chatFrameLayout);
        chatFrameLayout.setHorizontalGroup(
            chatFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(chatFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(chatFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, ChatSenderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .add(ChatSendButton))
                .addContainerGap())
        );
        chatFrameLayout.setVerticalGroup(
            chatFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(chatFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(ChatSenderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(ChatSendButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addContainerGap())
        );

        chatFrame.setBounds(20, 0, 270, 300);
        jDesktopPane1.add(chatFrame, javax.swing.JLayeredPane.DEFAULT_LAYER);

        loginFrame.setClosable(true);
        loginFrame.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        loginFrame.setIconifiable(true);
        loginFrame.setTitle(bundle.getString("JMDIApplication.loginFrame.title")); // NOI18N
        loginFrame.setVisible(true);

        ServerAddressLabel.setText(bundle.getString("JMDIApplication.ServerAddressLabel.text")); // NOI18N

        ServerPortLabel.setText(bundle.getString("JMDIApplication.ServerPortLabel.text")); // NOI18N

        ServerAddressTextField.setText(bundle.getString("JMDIApplication.ServerAddressTextField.text")); // NOI18N
        ServerAddressTextField.setToolTipText(bundle.getString("JMDIApplication.ServerAddressLabel.toolTipText")); // NOI18N

        ServerPortTextField.setText(bundle.getString("JMDIApplication.ServerPortTextField.text")); // NOI18N

        LogInButton.setText(bundle.getString("JMDIApplication.LogInButton.text")); // NOI18N
        LogInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogInButtonActionPerformed(evt);
            }
        });

        LogOutButton.setText(bundle.getString("JMDIApplication.LogOutButton.text")); // NOI18N
        LogOutButton.setEnabled(false);
        LogOutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogOutButtonActionPerformed(evt);
            }
        });

        UserNameLabel.setText(bundle.getString("JMDIApplication.UserNameLabel.text")); // NOI18N

        UserNameTextField.setText(bundle.getString("JMDIApplication.UserNameTextField.text")); // NOI18N

        UserPassLabel.setText(bundle.getString("JMDIApplication.UserPassLabel.text")); // NOI18N

        UserPassTextField.setText(bundle.getString("JMDIApplication.UserPassTextField.text")); // NOI18N

        org.jdesktop.layout.GroupLayout loginFrameLayout = new org.jdesktop.layout.GroupLayout(loginFrame.getContentPane());
        loginFrame.getContentPane().setLayout(loginFrameLayout);
        loginFrameLayout.setHorizontalGroup(
            loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(loginFrameLayout.createSequentialGroup()
                .add(69, 69, 69)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(ServerAddressLabel)
                    .add(ServerPortLabel)
                    .add(UserNameLabel)
                    .add(UserPassLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(UserPassTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ServerPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(UserNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ServerAddressTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(126, 126, 126))
            .add(loginFrameLayout.createSequentialGroup()
                .add(97, 97, 97)
                .add(LogInButton)
                .add(18, 18, 18)
                .add(LogOutButton)
                .addContainerGap(175, Short.MAX_VALUE))
        );
        loginFrameLayout.setVerticalGroup(
            loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(loginFrameLayout.createSequentialGroup()
                .add(33, 33, 33)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ServerAddressTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ServerAddressLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ServerPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ServerPortLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(UserNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(UserNameLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(UserPassTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(UserPassLabel))
                .add(21, 21, 21)
                .add(loginFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(LogInButton)
                    .add(LogOutButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        loginFrame.setBounds(190, 210, 370, 220);
        jDesktopPane1.add(loginFrame, javax.swing.JLayeredPane.DEFAULT_LAYER);

        fileMenu.setText(bundle.getString("JMDIApplication.fileMenu.text")); // NOI18N

        saveMenuItem.setText(bundle.getString("JMDIApplication.saveMenuItem.text")); // NOI18N
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setText(bundle.getString("JMDIApplication.saveAsMenuItem.text")); // NOI18N
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setText(bundle.getString("JMDIApplication.exitMenuItem.text")); // NOI18N
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText(bundle.getString("JMDIApplication.editMenu.text")); // NOI18N

        cutMenuItem.setText(bundle.getString("JMDIApplication.cutMenuItem.text")); // NOI18N
        editMenu.add(cutMenuItem);

        copyMenuItem.setText(bundle.getString("JMDIApplication.copyMenuItem.text")); // NOI18N
        editMenu.add(copyMenuItem);

        pasteMenuItem.setText(bundle.getString("JMDIApplication.pasteMenuItem.text")); // NOI18N
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setText(bundle.getString("JMDIApplication.deleteMenuItem.text")); // NOI18N
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        viewMenu.setText(bundle.getString("JMDIApplication.viewMenu.text")); // NOI18N

        LoginMenuItem.setText(bundle.getString("JMDIApplication.LoginMenuItem.text")); // NOI18N
        LoginMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginMenuItemActionPerformed(evt);
            }
        });
        viewMenu.add(LoginMenuItem);

        chatMenuItem.setText(bundle.getString("JMDIApplication.chatMenuItem.text")); // NOI18N
        viewMenu.add(chatMenuItem);

        logMenuItem.setText(bundle.getString("JMDIApplication.logMenuItem.text")); // NOI18N
        logMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logMenuItemActionPerformed(evt);
            }
        });
        viewMenu.add(logMenuItem);

        menuBar.add(viewMenu);

        helpMenu.setText(bundle.getString("JMDIApplication.helpMenu.text")); // NOI18N

        contentMenuItem.setText(bundle.getString("JMDIApplication.contentMenuItem.text")); // NOI18N
        helpMenu.add(contentMenuItem);

        aboutMenuItem.setText(bundle.getString("JMDIApplication.aboutMenuItem.text")); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusbar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 772, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                .add(2, 2, 2)
                .add(statusbar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        getAccessibleContext().setAccessibleName(bundle.getString("JMDIApplication.AccessibleContext.accessibleName")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LogInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogInButtonActionPerformed
        SocketAddress address = parseSocketAddress(ServerAddressTextField.getText(), ServerPortTextField.getText());
        String username = UserNameTextField.getText();
        String userpass = String.valueOf(UserPassTextField.getPassword());
        handler = new SwingClientHandler(JMDIApplication.this);
        client = new SwingClientSupport(handler);
        if (!client.connect(new NioSocketConnector(), address,username,userpass)) {
            JOptionPane.showMessageDialog(JMDIApplication.this,
                    "Could not connect to " + ServerAddressTextField.getText() + ". ");
            writeLogFrameln("Could not connect to " + ServerAddressTextField.getText() + ".");
        } else {
            writeLogFrameln("已连接到 " + ServerAddressTextField.getText() + ".");
            LogInButton.setEnabled(false);
            LogOutButton.setEnabled(true);
        }
    }//GEN-LAST:event_LogInButtonActionPerformed

    private void logMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logMenuItemActionPerformed
//        logFrame.setVisible(true);
        logFrame.show();
        try {
            logFrame.setSelected(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(JMDIApplication.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }//GEN-LAST:event_logMenuItemActionPerformed

    private void LogOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogOutButtonActionPerformed
        // 登出处理
        client.quit();
        LogInButton.setEnabled(true);
        LogOutButton.setEnabled(false);
    }//GEN-LAST:event_LogOutButtonActionPerformed

    private void ChatSendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChatSendButtonActionPerformed
        String toSend = ChatSenderTextField.getText();
        client.broadcast(toSend);
        ChatLogTextArea.append("我说：" + toSend);
        ChatLogTextArea.append(NEWLINE);
    }//GEN-LAST:event_ChatSendButtonActionPerformed

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
        WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        processWindowEvent(we);
//        System.exit(0);
    }// GEN-LAST:event_exitMenuItemActionPerformed

    private void LoginMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_LoginMenuItemActionPerformed
        loginFrame.show();
        try {
            loginFrame.setSelected(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(JMDIApplication.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }// GEN-LAST:event_LoginMenuItemActionPerformed

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JMDIApplication().setVisible(true);
            }
        });
    }

    private SocketAddress parseSocketAddress(String address, String s_port) {
        address = address.trim();
        s_port = s_port.trim();
        return new InetSocketAddress(address, parseInt(s_port));
    }

    private int parseInt(String s) {
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Illegal port number: " + s);
        }
    }

    /**
     * @return the connector
     */
    public NioSocketConnector getConnector() {
        if (connector == null) {
            connector = new NioSocketConnector();
        }
        return connector;
    }

    private void writeLogFrame(String msg) {
        writeLogFrame(msg, "");
    }

    private void writeLogFrameln(String msg) {
        writeLogFrame(msg, NEWLINE);
    }

    private void writeLogFrame(String msg, String suffix) {
        sb_log.append(msg);
        sb_log.append(suffix);
        LogTextPane.setText(sb_log.toString());

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea ChatLogTextArea;
    private javax.swing.JButton ChatSendButton;
    private javax.swing.JTextField ChatSenderTextField;
    private javax.swing.JButton LogInButton;
    private javax.swing.JButton LogOutButton;
    private javax.swing.JTextPane LogTextPane;
    private javax.swing.JMenuItem LoginMenuItem;
    private javax.swing.JLabel ServerAddressLabel;
    private javax.swing.JTextField ServerAddressTextField;
    private javax.swing.JLabel ServerPortLabel;
    private javax.swing.JTextField ServerPortTextField;
    private javax.swing.JLabel UserNameLabel;
    private javax.swing.JTextField UserNameTextField;
    private javax.swing.JLabel UserPassLabel;
    private javax.swing.JPasswordField UserPassTextField;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JInternalFrame chatFrame;
    private javax.swing.JMenuItem chatMenuItem;
    private javax.swing.JMenuItem contentMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JInternalFrame logFrame;
    private javax.swing.JMenuItem logMenuItem;
    private javax.swing.JInternalFrame loginFrame;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JPanel statusbar;
    private javax.swing.JMenu viewMenu;
    // End of variables declaration//GEN-END:variables
    // My Variables declaration
    private SwingClientSupport client;
    private SwingClientHandler handler;
    private NioSocketConnector connector;
    private StringBuilder sb_log = new StringBuilder();
    public static final String NEWLINE = System.getProperty("line.separator");
    // End of My variables declaration

    public void connected() {
    }

    public void loggedIn(String msg) {
        writeLogFrameln(msg);
    }

    public void loggedOut(String msg) {
        writeLogFrameln(msg);
        writeLogFrameln("系统登出");
    }

    public void disconnected() {
    }

    public void messageReceived(byte[] message) {
    }

    public void messageBroadCasted(byte[] message) {
        ChatLogTextArea.append("系统广播：" + new String(message));
        ChatLogTextArea.append(NEWLINE);
    }

    public void error(byte[] message) {
    }

    /***
     * 重写此方法让用户确认是否需要关闭
     * */
    @Override
    protected void processWindowEvent(WindowEvent e) {
        boolean flag = false;
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            //关闭的提示选择
            int result = JOptionPane.showConfirmDialog(
                    this, "确认要关闭吗？", "温馨提示",
                    JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.NO_OPTION) {
                //不关闭，系统托盘？？？？
                flag = true;
            } else {
                if (client != null) {
                    client.quit();
                }
                //关闭的处理
            }
        }
        if (!flag) {
            //点击的了YES,那么交给上面去处理关闭的处理
            super.processWindowEvent(e);
        }
    }

    private void userinitComponents() {
        UserNameTextField.setDocument(new LenghtLimitedDocument(15));
    }
}
